import java.security.Security;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPTransport;


public class ServidorMailsAuntenticado extends ServidorMails {

	public ServidorMailsAuntenticado(String email, String password, String smtp, String puerto) {
		super(email, password, smtp, puerto);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void enviar(Message mensaje) {
		try {
			Transport t = this.session.getTransport("smtps");
			t.connect(this.email, this.password);
			t.send(mensaje, mensaje.getAllRecipients());
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Override
	public Session getSession() {
		/*props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", this.smtp);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "25");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");*/
		
		props.put("mail.smtp.host", this.smtp);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.user", this.email);
		props.put("mail.smtp.password", this.password);
		props.put("mail.smtp.port", this.puerto);
	    
		SMTPAuthenticator auth = new SMTPAuthenticator(this.email, this.password);
		this.session = Session.getDefaultInstance(props, auth);
		return session;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServidorMailsAuntenticado serv = new ServidorMailsAuntenticado("mab_787@yahoo.com.ar", "klapaucius+85", "smtp.mail.yahoo.com.ar", "25");
		Message m = new MimeMessage(serv.getSession());
		try {
			m.setSubject("ASUNTO");
			m.setContent("MENSAJE", "text/plain");
			m.addRecipient(RecipientType.BCC, new InternetAddress("mab_787@yahoo.com.ar"));
			m.addRecipient(RecipientType.BCC, new InternetAddress("mab_787@hotmail.com"));
			m.addRecipient(RecipientType.TO, new InternetAddress("mbritos@alumnos.exa.unicen.edu.ar"));
			m.addRecipient(RecipientType.BCC, new InternetAddress("matias.rizzi@gmail.com"));
			m.setFrom(new InternetAddress("mab_787@hotmail.com"));
			serv.enviar(m);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}
